package com.simple.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.simple.entity.CodeResult;
import com.simple.entity.StatusCode;
import com.simple.entity.SysRole;
import com.simple.entity.SysUser;
import com.simple.service.SysRoleService;
import com.simple.service.SysUserService;
import com.simple.utils.EncryptionUtils;
import com.simple.utils.SysConfig;
import com.simple.utils.TableUtils;

/**
 * 用户管理
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
	
	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysRoleService sysRoleService;

    //用户页
    @RequestMapping("/sysUser.html")
    public String sysUser() {
        return "sysUser";
    }
    //用户列表
    @RequiresPermissions("sysUser:view")
    @RequestMapping("/sysUser/list.html")
    @ResponseBody
    public JSONObject tableSysUser(){
        List<SysUser> list=sysUserService.findAll();
        return TableUtils.tableResult(list);
    }
    //删除用户
    @RequiresPermissions("sysUser:edit")
    @RequestMapping("/sysUser/delete/{id}.html")
    @ResponseBody
    public CodeResult deleteUser(@RequestParam("id")Integer id) {
    	sysUserService.deleteUserById(id);
    	return CodeResult.success(StatusCode.DELETE_SUCCESS);
    }
    //激活用户
    @RequiresPermissions("sysUser:edit")
	@RequestMapping("/sysUser/activationUser.html")
	@ResponseBody
	public CodeResult activationUser(@RequestParam("status")Integer status,@RequestParam("userId")Integer userId){
		sysUserService.activationUser(status,userId);
		return CodeResult.success();
	}
    //获取角色
	@RequestMapping("/sysUser/getSysRole.html")
	public String getSysRole(@RequestParam("id")Integer id,Model model) {
		List<SysRole> roleList=sysRoleService.findAll();
		model.addAttribute("list", roleList);
		if(null!=id) {
			SysUser user=sysUserService.findByUserId(id);
			if(null!=user.getSysRole()) {
				model.addAttribute("roleId", user.getSysRole().getId());
			}
			model.addAttribute("id", id);
		}
		return "sysUserEdit";
	}
	//关联角色
	@RequestMapping("/sysUser/relationRole.html")
	@ResponseBody
	public CodeResult relationRole(@RequestParam("id")Integer id,@RequestParam("roleId")Integer roleId,Model model) {
		logger.info("==>id:"+id+",roleId"+roleId);
		sysUserService.relationRole(id, roleId);
		return CodeResult.success();
	}
    
	//个人资料
	@RequestMapping("/sysUserInfo.html")
	public String sysUserInfo(Model model,HttpServletRequest request) {
		SysUser sysUser=sysUserService.selectUserName(SysConfig.getUsername());
		String jsonSysUser=JSON.toJSONString(sysUser);
		model.addAttribute("sysUser", jsonSysUser);
		return "sysUserInfo";
	}
	//验证旧密码
	@RequestMapping("/sysUser/oldPwdVerify.html")
	@ResponseBody
	public CodeResult oldPwdVerify(@RequestParam("oldPassword")String oldPassword) {
		String encryptionPwd=EncryptionUtils.shiroMd5(oldPassword);
		if(SysConfig.getUser().getPassword().equals(encryptionPwd)) {
			return CodeResult.success();
		}
		return CodeResult.fail(StatusCode.FAIL);
	}
	
	//更改密码
	@RequiresPermissions("sysUser:edit")
	@RequestMapping("/sysUser/modifyPassword.html")
	@ResponseBody
	public CodeResult modifyPassword(@RequestParam("password")String password) {
		sysUserService.modifyPassword(password);
		return CodeResult.success();
	}
	
}
